Vue.use(vant.Lazyload);
var vm = new Vue({
    //挂载点
    el: '#app',
    //数据集合
    data() {
        return {
            currentStep: 0,
            firstStep: true,
            secondStep: false,
            thirdStep: false,

            //显示选择性别pop
            showSelectGender: false,
            //显示选择时间pop
            showSelectTime: false,
            //显示选择结算方式
            showSelectSettlement: false,
            //显示选择地区
            showSelectArea: false,

            //工作详细
            jobInfo:{
                id: jobId,
                work_type: 0,
                work_typeCh: '',
                title: '',
                salary: '',
                salary_unit: '',
                salary_unitCh: '',
                need_worker: '',
                mobile: '',
                contacts: '',
                work_time: '',
                work_timeCh: '',
                gender: 0,
                genderCh:'男女不限',
                provinces_name: '',
                address: '',
                settlement_type: '',
                settlement_typeCh: '',
                content: '',
                requirement: '',
                work_welfare: '',
                extend_id: 0

            },

            jobsGender:[],
            jobsType:[],
            moneyUnit: [],
            settlementList: [],
            selectArea: {},

            selectMinDate: new Date(),

            phone:{
                error: '',
            }


        }
    },

    //创建时执行 操作
    created : function (){
        //获取兼职类型列表
        axiosRequest.get('/api/job/jobType').then(function (response) {
            if (response.code == 200) {
                response.data.forEach(function (item) {
                    item.style = '';
                });
                vm.$data.jobsType = response.data;

                vm.fillChoseJobType();
            } else {
                vant.Notify({ type: 'danger', message: response.msg });
            }
        });

        //获取系统中开放的地区
        axiosRequest.get('/api/common/openArea').then(function (response) {
            if (response.code == 200) {
                vm.$data.selectArea = response.data;
            }
        });

        //获取发布兼职需要的数据
        axiosRequest.get('/api/job/publishNeedData').then(function (response) {
            if (response.code == 200) {
                //添加数据
                response.data.moneyUnit.forEach(function (item) {
                    item.class = ['money-unit-btn'];
                });
                vm.$data.jobsGender = response.data.gender;
                vm.$data.moneyUnit = response.data.moneyUnit;
                vm.$data.settlementList = response.data.settlement;
            }
        });


        //获取兼职详情
        if (this.jobInfo.id) {
            var params = {
                id: this.jobInfo.id,
                extend: 'content,publishUser'
            };
            axiosRequest.get('/api/job/detail',{params:params}).then(function (response) {
                if (response.code == 200) {
                    vm.$data.jobInfo = response.data;
                    vm.$data.jobInfo.content = response.data.extendContent.content;
                    vm.$data.jobInfo.requirement = response.data.extendContent.requirement;
                    vm.$data.jobInfo.work_welfare = response.data.extendContent.work_welfare;
                    vm.$data.jobInfo.salary = parseFloat(vm.$data.jobInfo.salary);
                    vm.fillChoseWorkTime();
                    vm.fillChoseMoneyUnit();
                    vm.fillChoseJobType();
                } else {
                    vant.Toast({
                        message: response.msg,
                        onClose: function () {
                            history.back();
                        }
                    });
                }
            });
        }


    },

    mounted(){
    },

    //计算属性
    computed:{
        workLocation(){

        },
        getSelectGender() {
            return this.jobInfo.genderCh ? this.jobInfo.genderCh : '男女不限';
        },
        getSelectTime()
        {
            return this.jobInfo.work_timeCh ? this.jobInfo.work_timeCh : '请选择日期';
        },
        getSettlement(){
            return this.jobInfo.settlement_typeCh ? this.jobInfo.settlement_typeCh : '请选择结算方式';
        },
        getLocation(){
            return this.jobInfo.provinces_name ? this.jobInfo.provinces_name : '请选择区域';
        },
        getSetp3Location(){
            return this.jobInfo.provinces_name + ' ' + this.jobInfo.address;
        }
    },



    //方法集合 在 `methods` 对象中定义方法
    methods: {
        //返回上一页
        goBack(){
            history.back();
        },


        //检测手机号码
        checkPhoneNum(){
            var reg = /^1(1|3|4|5|6|7|8|9)\d{1}[-]?\d{4}[-]?\d{4}$/;
            if(!reg.test(this.jobInfo.mobile)){
                this.phone.error = '请输入正确的电话号码!';
            } else {
                this.phone.error = '';
            }
        },
        //编辑 - 填充选择好的兼职类型
        fillChoseJobType(){
            if (this.jobInfo.id) {
                this.jobsType.forEach(function (item) {
                    if (item.id == vm.$data.jobInfo.work_type) {
                        item.style = 'chosed-job-type';
                        vm.$data.jobInfo.work_typeCh = item.name;
                    }
                });
            }
        },
        //编辑 - 填充选择好的薪资单位
        fillChoseMoneyUnit(){
            if (this.jobInfo.id) {
                this.moneyUnit.forEach(function (item) {
                    if (item.index == vm.$data.jobInfo.salary_unit) {
                        item.class = ['money-unit-btn', 'chose-money-unit-btn'];
                    }
                });
            }
        },
        //编辑 - 填充时间
        fillChoseWorkTime(){
            if (this.jobInfo.id) {
                this.jobInfo.work_time = new Date(parseInt(this.jobInfo.work_time) * 1000);
            }
        },


        clickJobType(choseItem){
            this.jobsType.forEach(function (item) {
                item.style = '';
            });
            choseItem.style = 'chosed-job-type';
            this.jobInfo.work_type = choseItem.id;
            this.jobInfo.work_typeCh = choseItem.name;
        },

        //第一步
        firstStepBtn(){
            if (!this.jobInfo.work_type) {
                vant.Notify({type: 'danger', message: '请先选择兼职类型,在进行下一步操作!'});
                vant.Toast('请先选择兼职类型!');

            } else {
                this.firstStep = false;
                this.secondStep = true;
                this.currentStep = 1;
            }
        },
        backFirstStopBtn(){
            this.firstStep = true;
            this.secondStep = false;
            this.currentStep = 0;
        },


        //第二步
        secondStepBtn(){
            var mustNotEmpey = [
                {
                    field: 'title',
                    errorMsg: '请填写标题信息!',
                },
                {
                    field: 'salary',
                    errorMsg: '请填写待遇薪资！',
                },
                {
                    field: 'salary_unit',
                    errorMsg: '请选择待遇薪资方式!',
                },
                {
                    field: 'need_worker',
                    errorMsg: '请填写招聘人数！',
                },
                {
                    field: 'mobile',
                    errorMsg: '请填写招聘方联系方式!',
                },
                {
                    field: 'contacts',
                    errorMsg: '请填写招聘方联系人!',
                },
                {
                    field: 'work_time',
                    errorMsg: '请填写兼职时间!',
                },
                {
                    field: 'content',
                    errorMsg: '请填写兼职内容!',
                },
                {
                    field: 'address',
                    errorMsg: '请填写兼职具体地点!',
                },
                {
                    field: 'settlement_type',
                    errorMsg: '请选择结算方式!',
                },
                {
                    field: 'requirement',
                    errorMsg: '请填写兼职要求!',
                },
                {
                    field: 'provinces_name',
                    errorMsg: '请选择兼职所在区域！',
                },
            ];
            for(var item of mustNotEmpey) {
                if (!this.jobInfo[item.field]) {
                    vant.Toast(item.errorMsg);
                    return false;
                }
            }
            this.secondStep = false;
            this.thirdStep = true;
            this.currentStep = 2;
        },

        backSecondStopBtn(){
            this.thirdStep = false;
            this.secondStep = true;
            this.currentStep = 1;
        },

        //保存到草稿
        saveJobDraft(){
            var postData = this.getPostData();

            axiosRequest.post('/api/job/saveDraft', postData).then(function (response) {
                if (response.code == 200) {
                    vm.jobInfo.id = response.data.jobId;
                    vm.jobInfo.extend_id = response.data.extendId;
                    vant.Toast({
                        message: '已保存为草稿！',
                        onClose(){
                            window.location.href = '/index/Usercenter/publishJobList?type=0';
                        }
                    });
                } else {
                    vant.Toast('保存失败！');
                    vant.Notify({type:'danger', message:response.msg});
                }
            });
            return true;
        },

        getPostData(){
            var jobPostData = {
                extendId: this.jobInfo.extend_id,
                id: this.jobInfo.id,
                work_type: this.jobInfo.work_type,
                contacts: this.jobInfo.contacts,
                mobile: this.jobInfo.mobile,
                title: this.jobInfo.title,
                work_time: (Date.parse(this.jobInfo.work_time) / 1000),
                address: this.jobInfo.address,
                provinces_name: this.jobInfo.provinces_name,
                salary: this.jobInfo.salary,
                salary_unit: this.jobInfo.salary_unit,
                need_worker: this.jobInfo.need_worker,
                settlement_type: this.jobInfo.settlement_type,
                gender: this.jobInfo.gender,
                content: this.jobInfo.content,
                requirement: this.jobInfo.requirement,
                work_welfare: this.jobInfo.work_welfare
            };
            return jobPostData;
        },

        //保存发布
        publishJob(){
            var postData = this.getPostData();
            axiosRequest.post('/api/job/publishJob', postData).then(function (response) {
                if (response.code == 200) {
                    vant.Toast({
                        message: '发布成功,请等待审核!',
                        onClose(){
                            window.location.href = '/index/Usercenter/publishJobList?type=1';
                        }
                    });

                } else {
                    if (response.code == 200006) {
                        vant.Dialog.confirm({
                            message: response.msg,
                            confirmButtonText: '去充值',
                            cancelButtonText: '先保存草稿'
                        }).then(() => {
                            window.location.href = '/index/Userwallet/buyBean';
                        }).catch(() => {
                        });
                    } else {
                        vant.Notify({type:'danger', message:response.msg});
                    }
                }
            });
        },


        /** popup  选择 工具 start */

        //选择男女
        clickShowSelectGender(){
            this.showSelectGender = true;
        },
        confirmSelectGender(item, index){
            this.jobInfo.gender = index;
            this.jobInfo.genderCh = item;
            this.showSelectGender = false;
        },

        //选择时间
        clickShowSelectTime(){
            this.showSelectTime = true;
        },
        confirmSelectTime(value){
            var year = value.getFullYear() + '年';
            var month = value.getMonth() + '月';
            var day = value.getDate() + '日';
            this.jobInfo.work_timeCh = year + month + day;
            this.jobInfo.work_time = value;
            this.showSelectTime = false;
        },
        formatDate(type, value) {
            if (type === 'year') {
                return `${value}年`;
            } else if (type === 'month') {
                return `${value}月`
            }
            return `${value}日`;
        },

        //选择结算方式
        clickShowSelectSettlement(){
            this.showSelectSettlement = true;
        },
        confirmSelectSettlement(item){
            this.jobInfo.settlement_type = item.index;
            this.jobInfo.settlement_typeCh = item.text;
            this.showSelectSettlement = false;
        },

        //选择兼职地区
        clickShowSelectArea(){
            this.showSelectArea = true;
        },
        confirmSelectArea(item){
            this.jobInfo.provinces_name = '';
            item.forEach(function(one){
                vm.$data.jobInfo.provinces_name += ' ' + one.name;
            });
            this.showSelectArea = false;
        },

        //选择薪资单位
        choseMoneyUnit(item){
            this.moneyUnit.forEach(function(one){
                one.class = ['money-unit-btn'];
            });
            this.jobInfo.salary_unit = item.index;
            this.jobInfo.salary_unitCh = item.text;
            item.class = ['money-unit-btn', 'chose-money-unit-btn'];
        }
        /** popup  选择 工具 start */

    }

});
